<?php
include 'util.php';

$apikey = "0d5cbb6f-218d-4f91-b32c-1f51607bb4bd";
$secret = "my-super-secret";

if (isset($_REQUEST['callee'])) {
    // devzone call $_REQUEST['callee']
    $params = "callee=".$_REQUEST['callee'];
    $result = invokeApi ("Call", $apikey, $secret, $params);    
    $callId = $result['content'];

    $status = "<b>Status:</b><br/>";
    if ($result['httpcode'] == "200") {
        $status .= "Success: Call<br/>";
    } else {
        $status .= "Failure: Call<br/>";
    }

    // devzone plays every announcement that is not empty
    if ($_REQUEST['audio1'] != "") {
        $params = "callId=".$callId."&announcement=".$_REQUEST['audio1'];
        $result = invokeApi ("Play", $apikey, $secret, $params);

        if ($result['httpcode'] == "200") {
            $status .= "Success: Audio 1<br/>";
        } else {
            $status .= "Failure: Audio 1<br/>";
        }
    }

    if ($_REQUEST['audio2'] != "") {
        $params = "callId=".$callId."&announcement=".$_REQUEST['audio2'];
        $result = invokeApi ("Play", $apikey, $secret, $params);
        
        if ($result['httpcode'] == "200") {
            $status .= "Success: Audio 2<br/>";
        } else {
            $status .= "Failure: Audio 2<br/>";
        }
    }

    if ($_REQUEST['audio3'] != "") {
        $params = "callId=".$callId."&announcement=".$_REQUEST['audio3'];
        $result = invokeApi ("Play", $apikey, $secret, $params);
        
        if ($result['httpcode'] == "200") {
            $status .= "Success: Audio 3<br/>";
        } else {
            $status .= "Failure: Audio 3<br/>";
        }
    }

    if ($_REQUEST['audio4'] != "") {
        $params = "callId=".$callId."&announcement=".$_REQUEST['audio4'];
        $result = invokeApi ("Play", $apikey, $secret, $params);
        
        if ($result['httpcode'] == "200") {
            $status .= "Success: Audio 4<br/>";
        } else {
            $status .= "Failure: Audio 4<br/>";
        }
    }

    if ($_REQUEST['audio5'] != "") {
        $params = "callId=".$callId."&announcement=".$_REQUEST['audio5'];
        $result = invokeApi ("Play", $apikey, $secret, $params);
        
        if ($result['httpcode'] == "200") {
            $status .= "Success: Audio 5<br/>";
        } else {
            $status .= "Failure: Audio 5<br/>";
        }
    }

    // devzone hangs up
    $params = "callId=".$callId."&immediate=false";
    $result = invokeApi ("Hangup", $apikey, $secret, $params);
       
    if ($result['httpcode'] == "200") {
        $status .= "Success: Hangup<br/>";
    } else {
        $status .= "Failure: Hangup<br/>";
    }
?>

<html><body>
    <?=$status?>
</body></html>

<?php
} else {
?>

<html><body><form>

    Phone # to call:<br/>
    <input type="text" name="callee" value=""/>
    <br/>

    Audio file #1 to play:<br/>
    <input type="text" name="audio1" value="file:///opt/id-code-is.wav"/>
    <br/>

    Audio file #2 to play:<br/>
    <input type="text" name="audio2" value="file:///opt/3.wav"/>
    <br/>

    Audio file #3 to play:<br/>
    <input type="text" name="audio3" value="file:///opt/1.wav"/>
    <br/>

    Audio file #4 to play:<br/>
    <input type="text" name="audio4" value="file:///opt/4.wav"/>
    <br/>

    Audio file #5 to play:<br/>
    <input type="text" name="audio5" value="file:///opt/5.wav"/>
    <br/>

    <input type="submit" value="Call and Play"/>

</form></body></html>

<?php
}
?>
